package com.jinbo.sxzq;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class GameActivity extends Activity {
    /*********手机的屏幕大小***************/
    public int windowsizex;
    public int windowsizey;

    GameDeployView gameDeployView;
    GameView gameView;
    LinearLayout linearLayout;
    Button star_game;
    GameControl gameControl;

    //图片资源
    Bitmap[] chess_bitmaps = new Bitmap[12];//棋子的图片资源
    int[] chess_bitmaps_id = {R.drawable.chess_shu,R.drawable.chess_nui,R.drawable.chess_hu,R.drawable.chess_tu,//棋子图片id
            R.drawable.chess_long,R.drawable.chess_she,R.drawable.chess_ma,R.drawable.chess_yang,
            R.drawable.chess_hou, R.drawable.chess_ji,R.drawable.chess_gou,R.drawable.chess_zhu};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN ,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindowSize();
        //接收传递过来的参数创建对应关卡的gamecontrol
        gameControl = new GameControl(getIntent().getFlags());//创建一个游戏控制对象并设置其关卡
        //初始化图片资源
        for(int i = 0 ; i < chess_bitmaps_id.length ; i++){
            chess_bitmaps[i] = BitmapFactory.decodeResource(getResources(),chess_bitmaps_id[i]);
        }
        initGameDeployView();
        this.setContentView(R.layout.activity_main);
        linearLayout = findViewById(R.id.gameview_container);
        linearLayout.addView(gameDeployView);
        star_game = findViewById(R.id.star_game);
        star_game.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*if(gameControl.gamestate == ProjectConstants.GameState.Game_DEPLOY){
                    linearLayout.removeView(gameDeployView);
                    star_game.setText("重新部署");
                    gameControl.gamestate = ProjectConstants.GameState.GAME_PLAY;
                    linearLayout.addView(gameView);
                    return;
                }
                if(gameControl.gamestate == ProjectConstants.GameState.GAME_PLAY){
                    linearLayout.removeView(gameView);
                    star_game.setText("部署完成");
                    gameControl.gamestate = ProjectConstants.GameState.Game_DEPLOY;
                    linearLayout.addView(gameDeployView);
                    return;
                }*/
                initGameView();
                linearLayout.removeView(gameDeployView);
                linearLayout.addView(gameView);
            }
        });
    }

    /*********获取手机的屏幕大小***************/
    public void getWindowSize(){
        Display display = getWindowManager().getDefaultDisplay();
        Point displaysize = new Point();
        display.getSize(displaysize);//获取屏幕像素大小
        this.windowsizex = displaysize.x;
        this.windowsizey = displaysize.y;
    }

    public void initGameDeployView(){
        gameDeployView = new GameDeployView(this);
    }
    public void initGameView(){
        gameView = new GameView(this);
    }

    //创建消息控制界面转换
    Handler handler = new Handler() {
        public void handleMessage(Message msg){
            if(msg.what == 1){//加载部署界面
                if(gameView != null){
                    linearLayout.removeView(gameView);
                    gameView = null;
                }
                initGameDeployView();
                linearLayout.addView(gameDeployView);
            }else if(msg.what == 2){//加载游戏界面
                if(gameDeployView != null){
                    linearLayout.removeView(gameDeployView);
                    gameDeployView = null;
                }
                initGameView();
                linearLayout.addView(gameView);
            }
        }
    };
}
